package com.suixun.controller;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.Constants;
import com.suixun.entity.form.ParamForm;
import com.suixun.entity.page.PageModel;
import com.suixun.exception.OpServiceException;
import com.suixun.service.BannerService;
import com.suixun.util.Constant;
import com.suixun.vo.BannerVO;

import rop.thirdparty.org.apache.commons.lang3.StringUtils;

@Controller
@RequestMapping(value = "/banner")
public class BannerController extends BaseManager {

	@Resource
	private BannerService bannerService;

	@ResponseBody
	@RequestMapping(value = "getBannerPage.json")
	public Object getBannerPage(String param) {
		try {
			ParamForm paramForm = new Gson().fromJson(param, ParamForm.class);
			Integer pageNumber = null, pageCount = null, index = null, bannerRange = null;
			if (paramForm == null || StringUtils.isBlank(paramForm.getPagenumber())) {
				pageNumber = 1;
			} else {
				pageNumber = Integer.parseInt(paramForm.getPagenumber());
			}
			if (paramForm == null || StringUtils.isBlank(paramForm.getPagecount())) {
				pageCount = 15;
			} else {
				pageCount = Integer.parseInt(paramForm.getPagecount());
			}
			if (paramForm.getIndex() == null) {
				index = 1;
			}else {
				index = paramForm.getIndex();
			}
			if(paramForm.getBannerRange() == null) {
				bannerRange = 1;
			}else {
				bannerRange = paramForm.getBannerRange();
			}
			PageModel<BannerVO> pageModel = bannerService.getBannerPage(pageNumber, pageCount, index, bannerRange);
			if(CollectionUtils.isNotEmpty(pageModel.getQueryresult())){
				for(BannerVO vo : pageModel.getQueryresult()){
					vo.setBannerUrl(Constants.getImageUrl() + vo.getBannerUrl());
				}
			}
			return success(pageModel);
		} catch (Exception e) {
			e.printStackTrace();
			logger("BannerController getBannerPage:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	@ResponseBody
	@RequestMapping(value = "addBanner.json")
	public Object addBanner(String param) {
		try {
			BannerVO bannerVo = new Gson().fromJson(param, BannerVO.class);
			if (bannerVo == null) {
				return fail("数据为空");
			}
			String res = bannerService.addBanner(bannerVo);
			if (res != null) {
				return fail(res);
			}
			return success();
		} catch (Exception e) {
			logger("BannerController addBanner:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	@ResponseBody
	@RequestMapping(value = "updateBanner.json")
	public Object updateBanner(String param) {
		try {
			BannerVO bannerVo = new Gson().fromJson(param, BannerVO.class);
			String res = bannerService.updateBanner(bannerVo);
			if (res != null) {
				return fail(res);
			}
			return success();
		} catch (Exception e) {
			logger("BannerController updateBanner:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	@ResponseBody
	@RequestMapping(value = "deleteBanner.json")
	public Object deleteBanner(String param) {
		try {
			BannerVO bannerVo = new Gson().fromJson(param, BannerVO.class);
			if (bannerVo == null || StringUtils.isBlank(bannerVo.getBannerIds())) {
				return "数据为空";
			}
			String res = bannerService.deleteBanner(bannerVo.getBannerIds());
			if (res != null) {
				return fail(res);
			}
			return success();
		} catch (Exception e) {
			if (e instanceof OpServiceException) {
				return fail(e.getMessage());
			}
			logger("BannerController deleteBanner:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "getBanner.json")
	public Object getBanner(String param) {
		try {
			BannerVO bannerVo = new Gson().fromJson(param, BannerVO.class);
			if (bannerVo == null || bannerVo.getBannerId() == null) {
				return "数据为空";
			}
			BannerVO vo = bannerService.getBanner(bannerVo.getBannerId());
			if(vo != null){
				vo.setBannerUrl(Constants.getImageUrl() + vo.getBannerUrl());
			}
			return success(vo);
		} catch (Exception e) {
			logger("BannerController getBanner:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
}
